package tool.soft

import java.io.File
import command.CommandExec
import dao._
import models.Tables
import org.joda.time.DateTime
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool._
import tool.plot._
import tool.soft.AllSoft.SoftBT
import implicits.Implicits._
import models.Tables._
import play.api.data.FormBinding
import utils.Utils
import play.api.i18n.Messages
import shared.pojo.mission.HeatmapSoftData
import upickle.default._

/**
 * Created by yz on 15/9/2020
 */
object HeatmapSoft extends HeatmapSoftT with SoftBT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit matrixFileDataDao: MatrixFileDataDao, messages: Messages): List[FileInfoData] = {
    List(
      FileInfoData("heatmap.pdf", messages("heatmap"))
    )
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    val data = FormTool.heatmapForm.bindFromRequest().get
    data.lines().toTxtFile(Tool.getCalculateConfigTxtFile(workspaceDir))
    plot.Heatmap.initConfigFile(workspaceDir, idStr = "")
    Utils.t2ConfigFile(workspaceDir, data)
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    val userId = Tool.getUserId
    val data = FormTool.heatmapForm.bindFromRequest().get
    val hasRowGroup = if (data.hasRowGroup) messages("yes") else messages("no")
    val basicJson = ujson.Obj(messages("importVariableInformation") -> hasRowGroup)
    val selectColumnsJson = if (data.selectColumns.nonEmpty) {
      ujson.Obj(messages("selectColumnsForAnno") -> data.selectColumns.mkString(";"))
    } else {
      ujson.Obj()
    }
    val hasColGroup = if (data.hasColGroup) messages("yes") else messages("no")
    basicJson ++ selectColumnsJson ++ ujson.Obj(messages("importGroupInformation") -> hasColGroup)
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.heatmapResult(missionId)
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val plotCommands =List(
      plot.Heatmap.getCommand(workspaceDir, idStr = ""),
    )
    val runCommand = if (Utils.isWindows) plotCommands else plotCommands
    List(CommandData(workspaceDir, runCommand))
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.soft.heatmap(missionName, missionId)
  }

  override def getMission(implicit request: Request[AnyContent], matrixFileDataDao: MatrixFileDataDao, messages: Messages, formBinding: FormBinding): Tables.MissionRow = {
    val data = FormTool.heatmapForm.bindFromRequest().get
    val initInputs = List(data.file)
    val hasRowInputs = if (data.hasRowGroup) {
      initInputs ::: List(data.rowGroupFile)
    } else initInputs
    val hasColInputs = if (data.hasColGroup) {
      hasRowInputs ::: List(data.colGroupFile)
    } else hasRowInputs
    val inputs = hasColInputs
    val argJson = getArg
    MissionRow(id = 0, missionName = s"${data.missionName}", kind = nameStr, userId = userId,
      startTime = new DateTime(), state = "preparing", arg = argJson, input = inputs)
  }

  override def produceInputFile(workspaceDir: File)(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val userId = Tool.getUserId
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawDataFile(userId, data.file, rawDataFile)
    if (data.hasRowGroup) {
      Tool.produceRawVarInfoFile(userId, data.rowGroupFile, rawDataFile, new File(workspaceDir, "row_group.txt"),
        data.selectColumns)
    }
    if (data.hasColGroup) {
      val groupFile = new File(workspaceDir, "col_group.txt")
      Tool.produceRawGroupLines(userId, data.colGroupFile, rawDataFile).removeColumnsCanMore(List("Pair_ID")).
        toTxtFile(groupFile)
    }
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    Tool.transformRq.transform(reader[HeatmapSoftData])
  }


}

